<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');
// load base class if needed
require_once( APPPATH . 'controllers/base/OperatorBase.php' );

class barang extends ApplicationBase {

    // constructor
    public function __construct() {
        parent::__construct();
        // load model  
        $this->load->model('m_barang');
        $this->load->model('m_group_barang');
        $this->load->model('m_kategoribarang');
        $this->load->model('m_jenisbarang');
        $this->load->model('m_merk');
        $this->load->model('m_lokasi');
        $this->load->model('m_ruang');

        // load library
        $this->load->library('tnotification');
        $this->load->library('pagination');
        $this->load->library('phpexcel');

        // load helpers 
        $this->smarty->load_javascript("resource/js/fusioncharts/fusioncharts.js");
    }

    // welcome administrator
    public function index() {
        $this->_set_page_rule("R");

        // set template content
        $this->smarty->assign("template_content", "laporan/barang/index.html");

        //data group 
        $result = $this->m_group_barang->get_all_group_barang_laporan();
        $this->smarty->assign("rs_id", $result);

        // tot 
        $tot_lokasi = $this->m_lokasi->get_count_all_lokasi();
        $this->smarty->assign("tot_lokasi", $tot_lokasi);

        // notification
        $this->tnotification->display_notification();
        $this->tnotification->display_last_field();

        // output
        parent::display();
    }

    public function barang_per_group() {
        $this->_set_page_rule("R");

        // set template content
        $this->smarty->assign("template_content", "laporan/barang/barang_per_group.html");

        //data group 
        $result = $this->m_group_barang->get_all_group_barang_laporan();
        $this->smarty->assign("rs_id", $result);

        // notification
        $this->tnotification->display_notification();
        $this->tnotification->display_last_field();

        // output
        parent::display();
    }

    public function barang_per_kategori($id) {
        $this->_set_page_rule("R");

        // set template content
        $this->smarty->assign("template_content", "laporan/barang/barang_per_kategori.html");

        //data kategori by group barang
        $result = $this->m_kategoribarang->get_all_kategori_laporan($id);
        $this->smarty->assign("rs_id", $result);

        // notification
        $this->tnotification->display_notification();
        $this->tnotification->display_last_field();

        // output
        parent::display();
    }

    public function barang_per_jenis($id) {
        $this->_set_page_rule("R");

        // set template content
        $this->smarty->assign("template_content", "laporan/barang/barang_per_jenis.html");

        //data group 
        $this->smarty->assign("rs_detail", $this->m_kategoribarang->get_kategoribarang_by_id($id));

        // get search parameter
        $search = $this->tsession->userdata('data_search');
        $this->smarty->assign("search", $search);

        // search parameters
        $search['jenisbarang_cd'] = empty($search['jenisbarang_cd']) ? "" : $search['jenisbarang_cd'];
        $search['merk_cd'] = empty($search['merk_cd']) ? "" : $search['merk_cd'];
        $search['ruang_cd'] = empty($search['ruang_cd']) ? "" : $search['ruang_cd'];
        //$tgl = $tahun . "-" . $bulan;
        $params = array("%".$id."%", "%".$search['jenisbarang_cd']."%", 
                    "%".$search['merk_cd']."%", "%".$search['ruang_cd']."%");


        /* start of pagination --------------------- */
        // pagination
        $config['base_url'] = site_url("laporan/barang/barang_per_jenis/" . $id);
        $config['total_rows'] = $this->m_barang->count_detail_barang_by_kategoribarang_cd($params);
        $config['uri_segment'] = 5;
        $config['per_page'] = 20;
        $this->pagination->initialize($config);
        $pagination['data'] = $this->pagination->create_links();
        // pagination attribute
        $start = $this->uri->segment(5, 0) + 1;
        $end = $this->uri->segment(5, 0) + $config['per_page'];
        $end = (($end > $config['total_rows']) ? $config['total_rows'] : $end);
        $pagination['start'] = ($config['total_rows'] == 0) ? 0 : $start;
        $pagination['end'] = $end;
        $pagination['total'] = $config['total_rows'];
        // pagination assign value
        $this->smarty->assign("pagination", $pagination);
        $this->smarty->assign("no", $start);
        /* end of pagination ---------------------- */
        // get data
        $params = array("%". $id ."%", "%".$search['jenisbarang_cd']."%", "%".$search['merk_cd']."%", "%".$search['ruang_cd']."%", ($start - 1), $config['per_page']);
        $this->smarty->assign("rs_id", $this->m_barang->get_detail_barang_by_kategoribarang_cd($params));

        //set data search
        $this->smarty->assign("rs_merk", $this->m_merk->get_merk_by_kategoribarang_cd($id));
        $this->smarty->assign("rs_ruang", $this->m_ruang->get_all_ruang());
        $this->smarty->assign("rs_jenis", $this->m_jenisbarang->get_jenisbarang_by_kategoribarang($id));

        // notification
        $this->tnotification->display_notification();
        $this->tnotification->display_last_field();

        // output
        parent::display();
    }

    // proses pencarian
    public function proses_cari() {
        // set page rules
        $this->_set_page_rule("R");
        // data
        if ($this->input->post('save') == "Reset") {
            $this->tsession->unset_userdata('data_search');
        } else {
            $params = array(
                "jenisbarang_cd" => $this->input->post("jenisbarang_cd"),
                "merk_cd" => $this->input->post("merk_cd"),
                "ruang_cd" => $this->input->post("ruang_cd")
            );
            $this->tsession->set_userdata("data_search", $params);
        }
        //echo $this->input->post("jenisbarang_cd");
        // redirect
        redirect("laporan/barang/barang_per_jenis/" . $this->input->post("kategoribarang_cd"));
    }
    
    public function view_detail($id) {
        // get data
        $detail = $this->m_barang->get_barang_detail_by_id($id);
        $html = '<div class="table-info-box">
                    <h3>DATA BARANG</h3>
                    <table width="100%" class="table-info">
                        <tbody>  
                            <tr>
                                <th width="30%">No Inventaris</th>
                                <td width="70%">'.$detail['barang_noinventaris'].'</td>
                            </tr>  
                            <tr>
                                <th>Nama Barang</th>
                                <td>'.$detail['barang_nama'].'</td> 
                            </tr>
                        </tbody>
                    </table></div>';
        $html .= '<table width="100%" class="table-view" id="table-list-detail">
                <tbody>
                    <tr>
                        <td width="30%"><b>Group Barang</b></td> 
                        <td width="70%">'.$detail['gpbarang_name'].'</td> 
                    </tr>
                    <tr>
                        <td><b>Kategori Barang</b></td> 
                        <td>'.$detail['kategoribarang_name'].'</td> 
                    </tr>
                    <tr>
                        <td><b>Jenis Barang</b></td> 
                        <td>'.$detail['jenisbarang_name'].'</td> 
                    </tr>
                    <tr>
                        <td><b>Merk Barang</b></td> 
                        <td>'.$detail['merk_name'].'</td> 
                    </tr>
                    <tr>
                        <td><b>Ruang</b></td> 
                        <td>'.$detail['ruang_name'].'</td> 
                    </tr>
                    <tr>
                        <td><b>Tgl Pembelian</b></td> 
                        <td>'.$this->datetimemanipulation->get_full_date($detail['barang_tglbeli']).'</td> 
                    </tr>
                    <tr>
                        <td><b>Tgl Berakhir</b></td> 
                        <td>'.$this->datetimemanipulation->get_full_date($detail['barang_tglberakhir']).'</td> 
                    </tr>
                    <tr>
                        <td><b>Harga Pembelian</b></td> 
                        <td>'.number_format($detail['barang_harga_beli'],0,',','.').'</td> 
                    </tr>
                    <tr>
                        <td><b>Kondisi</b></td> 
                        <td>'.$detail['barang_kondisi'].'</td> 
                    </tr>
                    <tr>
                        <td><b>Status</b></td> 
                        <td>'.$detail['barang_status'].'</td> 
                    </tr>';
        $html .='</tbody>
                </table>';
        echo $html;
    }
    
    public function barang_excel($id){
        // set page rules
        $this->_set_page_rule("R");
        // style title header
        $styleTitleHeader = array(
            'font' => array(
                'name' => 'Times New Roman',
                'bold' => 'bold',
                'size' => 11
            ),
            'alignment' => array(
                'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER
            )
        );

        // style text
        $styleText = array(
            'font' => array(
                'name' => 'Times New Roman',
                'size' => 10
            )
        );
        

        // style desc title column
        $styleDescColumn = array(
            'font' => array(
                'name' => 'Times New Roman',
                'bold' => true,
                'size' => 10
            ),
            'alignment' => array(
                'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
                'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER
            ),
            'borders' => array(
                'allborders' => array(
                    'style' => PHPExcel_Style_Border::BORDER_THIN,
                    'color' => array('argb' => '000000'),
                ),
            )
        );

        // style content
        $styleContent = array(
            'font' => array(
                'name' => 'Times New Roman',
                'bold' => false,
                'size' => 10
            ),
            'borders' => array(
                'allborders' => array(
                    'style' => PHPExcel_Style_Border::BORDER_THIN,
                    'color' => array('argb' => '000000'),
                )
            )
        );

        // style border
        $styleBorder = array(
            'borders' => array(
                'allborders' => array(
                    'style' => PHPExcel_Style_Border::BORDER_THIN,
                    'color' => array('argb' => '000000'),
                )
            )
        );


        // style right alignment
        $styleRightAlignment = array(
            'alignment' => array(
                'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_RIGHT,
                'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER
            ),
        );

        // style left alignment
        $styleLeftAlignment = array(
            'alignment' => array(
                'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_LEFT,
                'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER
            ),
        );
        

        // style justify alignment
        $styleJustifyAlignment = array(
            'alignment' => array(
                'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_JUSTIFY,
                'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER
            ),
        );

        // style center alignment
        $styleCenterAlignment = array(
            'alignment' => array(
                'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
                'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER
            ),
        );


        // data
        $search = $this->tsession->userdata('detail_barang');
        $search['jenisbarang_cd'] = empty($search['jenisbarang_cd']) ? "" : $search['jenisbarang_cd'];
        $search['merk_cd'] = empty($search['merk_cd']) ? "" : $search['merk_cd'];
        $search['ruang_cd'] = empty($search['ruang_cd']) ? "" : $search['ruang_cd'];
        //param
        $params = array("%".$id."%", "%".$search['jenisbarang_cd']."%","%".$search['merk_cd']."%", "%".$search['ruang_cd']."%");
        $rs_id = $this->m_barang->get_all_barang_by_kategoribarang_cd($params);
        $rs_detail = $this->m_kategoribarang->get_kategoribarang_by_id($id);
        
        $this->phpexcel->setActiveSheetIndex(0)->getStyle('A')->getAlignment()->setWrapText(true);
        $this->phpexcel->setActiveSheetIndex(0)->getStyle('B')->getAlignment()->setWrapText(true);
        $this->phpexcel->setActiveSheetIndex(0)->getStyle('C')->getAlignment()->setWrapText(true);
        $this->phpexcel->setActiveSheetIndex(0)->getStyle('D')->getAlignment()->setWrapText(true);
        $this->phpexcel->setActiveSheetIndex(0)->getStyle('E')->getAlignment()->setWrapText(true);
        $this->phpexcel->setActiveSheetIndex(0)->getStyle('F')->getAlignment()->setWrapText(true);
        $this->phpexcel->setActiveSheetIndex(0)->getStyle('G')->getAlignment()->setWrapText(true);
        $this->phpexcel->setActiveSheetIndex(0)->getStyle('H')->getAlignment()->setWrapText(true);
        $this->phpexcel->setActiveSheetIndex(0)
                ->setCellValue('A1', "STMIK AMIKOM YOGYAKARTA")
                ->setCellValue('A2', "LAPORAN DATA BARANG")
                ->setCellValue('A4', 'No')
                ->setCellValue('B4', 'No Inventaris')
                ->setCellValue('C4', 'Nama Barang')
                ->setCellValue('D4', 'Kategori')
                ->setCellValue('E4', 'Jenis')
                ->setCellValue('F4', 'Merk')
                ->setCellValue('G4', 'Ruang')
                ->setCellValue('H4', 'Kondisi');
        $this->phpexcel->getActiveSheet()
                ->mergeCells('A1:H1')
                ->mergeCells('A2:H2')
                ->mergeCells('A3:H3');
        $this->phpexcel->setActiveSheetIndex(0)->getStyle('A1:H3')->applyFromArray($styleTitleHeader);
        $this->phpexcel->setActiveSheetIndex(0)->getStyle('A4:H4')->applyFromArray($styleDescColumn);
        // set width
        $this->phpexcel->getActiveSheet()->getColumnDimension('A')->setWidth(5.00);
        $this->phpexcel->getActiveSheet()->getColumnDimension('B')->setWidth(20.00);
        $this->phpexcel->getActiveSheet()->getColumnDimension('C')->setWidth(30.00);
        $this->phpexcel->getActiveSheet()->getColumnDimension('D')->setWidth(15.00);
        $this->phpexcel->getActiveSheet()->getColumnDimension('E')->setWidth(15.00);
        $this->phpexcel->getActiveSheet()->getColumnDimension('F')->setWidth(15.00);
        $this->phpexcel->getActiveSheet()->getColumnDimension('G')->setWidth(15.00);
        $this->phpexcel->getActiveSheet()->getColumnDimension('H')->setWidth(15.00);
        //set data
        $row = 5;
        $no = 1;
        foreach ($rs_id as $val) {
            $this->phpexcel->setActiveSheetIndex(0)->setCellValue('A' . $row, $no++);
            $this->phpexcel->setActiveSheetIndex(0)->setCellValue('B' . $row, $val['barang_noinventaris']);
            $this->phpexcel->setActiveSheetIndex(0)->setCellValue('C' . $row, $val['barang_nama']);
            $this->phpexcel->setActiveSheetIndex(0)->setCellValue('D' . $row, $val['kategoribarang_name']);
            $this->phpexcel->setActiveSheetIndex(0)->setCellValue('E' . $row, $val['jenisbarang_name']);
            $this->phpexcel->setActiveSheetIndex(0)->setCellValue('F' . $row, $val['merk_name']);
            $this->phpexcel->setActiveSheetIndex(0)->setCellValue('G' . $row, $val['lokasi_name']);
            $this->phpexcel->setActiveSheetIndex(0)->setCellValue('H' . $row, $val['ruang_name']);
            //style
            $this->phpexcel->setActiveSheetIndex(0)->getStyle('A' . $row)->applyFromArray($styleCenterAlignment);
            $this->phpexcel->setActiveSheetIndex(0)->getStyle('B' . $row . ':H' . $row)->applyFromArray($styleLeftAlignment);
            $this->phpexcel->setActiveSheetIndex(0)->getStyle('A' . $row . ':H' . $row)->applyFromArray($styleText);
            $this->phpexcel->setActiveSheetIndex(0)->getStyle('A' . $row . ':H' . $row)->applyFromArray($styleBorder);
            $row++;
        }
        
        // setting
        $this->phpexcel->getActiveSheet()->getPageSetup()->setOrientation(PHPExcel_Worksheet_PageSetup::ORIENTATION_LANDSCAPE);
        $this->phpexcel->getActiveSheet()->getPageSetup()->setPaperSize(PHPExcel_Worksheet_PageSetup::PAPERSIZE_A4);
        $name = 'Data Barang '.$rs_detail['kategoribarang_name'];
        $this->phpexcel->getActiveSheet()->setTitle($name);
        $this->phpexcel->setActiveSheetIndex(0);
        //--
        $file_name = 'DATA_BARANG_'.strtoupper(str_replace(" ", "_", $rs_detail['kategoribarang_name'])).'_'.date("d").'_'.date("m").'_'.date("Y");
        //--
//            header('Content-Type: application/vnd.ms-excel');
        header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
        header('Content-Disposition: attachment;filename=' . $file_name . '.xlsx');
        header('Cache-Control: max-age=0');
        // output
        $obj_writer = PHPExcel_IOFactory::createWriter($this->phpexcel, 'Excel2007');
        $obj_writer->save('php://output');
    }

    public function nilaiaset() {
        $this->_set_page_rule("R");

        // set template content
        $this->smarty->assign("template_content", "laporan/barang/nilaiaset.html");

        //data group 
        $result = $this->m_kategoribarang->get_all_kategori_graph();
        $this->smarty->assign("rs_id", $result);

        // notification
        $this->tnotification->display_notification();
        $this->tnotification->display_last_field();
        // output
        parent::display();
    }
    

    public function nilaiaset_detail($id) {
        $this->_set_page_rule("R");

        // set template content
        $this->smarty->assign("template_content", "laporan/barang/nilaiaset_detail.html");
        
        $search = $this->tsession->userdata('detail_barang');
        $search['jenisbarang_cd'] = empty($search['jenisbarang_cd']) ? "" : $search['jenisbarang_cd'];
        $search['merk_cd'] = empty($search['merk_cd']) ? "" : $search['merk_cd'];
        $search['ruang_cd'] = empty($search['ruang_cd']) ? "" : $search['ruang_cd'];
        //$tgl = $tahun . "-" . $bulan;
        $params = array("%".$id."%", "%".$search['jenisbarang_cd']."%", 
                    "%".$search['merk_cd']."%", "%".$search['ruang_cd']."%");
        
        //sent to display
        $this->smarty->assign("search", $search);
        
        /* start of pagination --------------------- */
        // pagination
        $config['base_url'] = site_url("laporan/barang/nilaiaset_detail/".$id);
        $config['total_rows'] = $this->m_barang->count_detail_barang_by_kategoribarang_cd($params);
        $config['uri_segment'] = 5;
        $config['per_page'] = 20;
        $this->pagination->initialize($config);
        $pagination['data'] = $this->pagination->create_links();
        // pagination attribute
        $start = $this->uri->segment(5, 0) + 1;
        $end = $this->uri->segment(5, 0) + $config['per_page'];
        $end = (($end > $config['total_rows']) ? $config['total_rows'] : $end);
        $pagination['start'] = ($config['total_rows'] == 0) ? 0 : $start;
        $pagination['end'] = $end;
        $pagination['total'] = $config['total_rows'];
        // pagination assign value
        $this->smarty->assign("pagination", $pagination);
        $this->smarty->assign("no", $start);
        /* end of pagination ---------------------- */

        // get data
        $params = array("%". $id ."%", "%".$search['jenisbarang_cd']."%", "%".$search['merk_cd']."%", "%".$search['ruang_cd']."%", ($start - 1), $config['per_page']);
        $result = $this->m_barang->get_detail_barang_by_kategoribarang_cd($params);
        $this->smarty->assign("rs_id", $result);
        
        //set data search
        $this->smarty->assign("rs_merk", $this->m_merk->get_merk_by_kategoribarang_cd($id));
        $this->smarty->assign("rs_ruang", $this->m_ruang->get_all_ruang());
        $this->smarty->assign("rs_jenis", $this->m_jenisbarang->get_jenisbarang_by_kategoribarang($id));
        $this->smarty->assign("rs_detail", $this->m_kategoribarang->get_kategoribarang_by_id($id));

        // notification
        $this->tnotification->display_notification();
        $this->tnotification->display_last_field();
        // output
        parent::display();
    }
    
    
    // proses filter
    public function proses_filter($id) {
        // set page rules
        $this->_set_page_rule("R");
        // data
        if ($this->input->post('save') == "Reset") {
            $this->tsession->unset_userdata('detail_barang');
        } else {
            $params = array(
                "jenisbarang_cd" => $this->input->post("jenisbarang_cd"),
                "merk_cd" => $this->input->post("merk_cd"),
                "ruang_cd" => $this->input->post("ruang_cd")
            );
            $this->tsession->set_userdata("detail_barang", $params);
        }
        // redirect
        redirect("laporan/barang/nilaiaset_detail/".$id);
    }
    
    public function view_nilaiaset($id) {
        // get data
        $detail = $this->m_barang->get_barang_detail_by_id($id);
        $data = $this->m_barang->get_all_penyusutan_by_barang_id($id);
        $html = '<div class="table-info-box">
                    <h3>DATA BARANG</h3>
                    <table width="100%" class="table-info">
                        <tbody>  
                            <tr>
                                <th width="25%">Nomor Inventaris</th>
                                <td width="75%">'.$detail['barang_noinventaris'].'</td>
                            </tr>  
                            <tr>
                                <th>Nama Barang</th>
                                <td>'.$detail['barang_nama'].'</td> 
                            </tr>
                        </tbody>
                    </table></div>';
        $html .= '<table width="100%" class="table-view" id="table-list-penyusutan">
                <tbody>
                    <tr>
                        <th width="4%">No</th> 
                        <th width="13%">Tgl Awal Penyusutan</th> 
                        <th width="13%">Tgl Akhir Penyusutan</th> 
                        <th width="10%">Waktu Penggunaan (bulan)</th>
                        <th width="15%">Penyusutan Per Bulan</th>
                        <th width="15%">Nilai Buku Awal</th> 
                        <th width="15%">Akumulasi Penyusutan</th>
                        <th width="15%">Nilai Buku Akhir</th>
                    </tr>';
        if (!empty($data)){
            $no = 1;
        foreach ($data as $val) {
            $style = "";
            if (($no % 2) <> 0) {
                $style = 'class="blink-row"';
            }
            $html .= '<tr '.$style.'>';
            $html .= '<td align="center">'.$no++.'</td>';
            $html .= '<td>'.$this->datetimemanipulation->get_full_date($val['penyusutandet_tgl_awal']).'</td>';
            $html .= '<td>'.$this->datetimemanipulation->get_full_date($val['penyusutandet_tgl_akhir']).'</td>';
            $html .= '<td align="center">'.  number_format($val['penyusutandet_jmlbulan'],0,",",".").'</td>';
            $html .= '<td align="right">'.  number_format($val['penyusutandet_perbulan'],0,",",".").'</td>';
            $html .= '<td align="right">'.  number_format($val['penyusutandet_nilai_awal'],0,",",".").'</td>';
            $html .= '<td align="right">'.  number_format(($val['penyusutandet_jmlbulan']*$val['penyusutandet_perbulan']),0,",",".").'</td>';
            $html .= '<td align="right">'.  number_format($val['penyusutandet_nilai_awal'],0,",",".").'</td>';
            $html .= '</tr>';
            
        }
        } else {
            $html .='<td colspan="8">Data Belum Tersedia</td>';
        }

        $html .='</tbody>
                </table>';
        echo $html;
    }
    
    public function nilaiaset_excel($id){
        // set page rules
        $this->_set_page_rule("R");
        // style title header
        $styleTitleHeader = array(
            'font' => array(
                'name' => 'Times New Roman',
                'bold' => 'bold',
                'size' => 11
            ),
            'alignment' => array(
                'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER
            )
        );

        // style text
        $styleText = array(
            'font' => array(
                'name' => 'Times New Roman',
                'size' => 10
            )
        );
        

        // style desc title column
        $styleDescColumn = array(
            'font' => array(
                'name' => 'Times New Roman',
                'bold' => true,
                'size' => 10
            ),
            'alignment' => array(
                'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
                'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER
            ),
            'borders' => array(
                'allborders' => array(
                    'style' => PHPExcel_Style_Border::BORDER_THIN,
                    'color' => array('argb' => '000000'),
                ),
            )
        );

        // style content
        $styleContent = array(
            'font' => array(
                'name' => 'Times New Roman',
                'bold' => false,
                'size' => 10
            ),
            'borders' => array(
                'allborders' => array(
                    'style' => PHPExcel_Style_Border::BORDER_THIN,
                    'color' => array('argb' => '000000'),
                )
            )
        );

        // style border
        $styleBorder = array(
            'borders' => array(
                'allborders' => array(
                    'style' => PHPExcel_Style_Border::BORDER_THIN,
                    'color' => array('argb' => '000000'),
                )
            )
        );


        // style right alignment
        $styleRightAlignment = array(
            'alignment' => array(
                'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_RIGHT,
                'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER
            ),
        );

        // style left alignment
        $styleLeftAlignment = array(
            'alignment' => array(
                'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_LEFT,
                'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER
            ),
        );
        

        // style justify alignment
        $styleJustifyAlignment = array(
            'alignment' => array(
                'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_JUSTIFY,
                'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER
            ),
        );

        // style center alignment
        $styleCenterAlignment = array(
            'alignment' => array(
                'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
                'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER
            ),
        );


        // data
        $search = $this->tsession->userdata('detail_barang');
        $search['jenisbarang_cd'] = empty($search['jenisbarang_cd']) ? "" : $search['jenisbarang_cd'];
        $search['merk_cd'] = empty($search['merk_cd']) ? "" : $search['merk_cd'];
        $search['ruang_cd'] = empty($search['ruang_cd']) ? "" : $search['ruang_cd'];
        //param
        $params = array("%".$id."%", "%".$search['jenisbarang_cd']."%","%".$search['merk_cd']."%", "%".$search['ruang_cd']."%");
        $rs_id = $this->m_barang->get_all_barang_by_kategoribarang_cd($params);
        $rs_detail = $this->m_kategoribarang->get_kategoribarang_by_id($id);
        
        $this->phpexcel->setActiveSheetIndex(0)->getStyle('A')->getAlignment()->setWrapText(true);
        $this->phpexcel->setActiveSheetIndex(0)->getStyle('B')->getAlignment()->setWrapText(true);
        $this->phpexcel->setActiveSheetIndex(0)->getStyle('C')->getAlignment()->setWrapText(true);
        $this->phpexcel->setActiveSheetIndex(0)->getStyle('D')->getAlignment()->setWrapText(true);
        $this->phpexcel->setActiveSheetIndex(0)->getStyle('E')->getAlignment()->setWrapText(true);
        $this->phpexcel->setActiveSheetIndex(0)->getStyle('F')->getAlignment()->setWrapText(true);
        $this->phpexcel->setActiveSheetIndex(0)
                ->setCellValue('A1', "STMIK AMIKOM YOGYAKARTA")
                ->setCellValue('A2', "LAPORAN ASET TETAP DAN AKUMULASI PENYUSUTAN")
                ->setCellValue('A4', 'No')
                ->setCellValue('B4', 'No Inventaris')
                ->setCellValue('C4', 'Nama Barang')
                ->setCellValue('D4', 'Ruang')
                ->setCellValue('E4', 'Nilai Perolehan')
                ->setCellValue('F4', 'Nilai Buku');
        $this->phpexcel->getActiveSheet()
                ->mergeCells('A1:F1')
                ->mergeCells('A2:F2')
                ->mergeCells('A3:F3');
        $this->phpexcel->setActiveSheetIndex(0)->getStyle('A1:F3')->applyFromArray($styleTitleHeader);
        $this->phpexcel->setActiveSheetIndex(0)->getStyle('A4:F4')->applyFromArray($styleDescColumn);
        // set width
        $this->phpexcel->getActiveSheet()->getColumnDimension('A')->setWidth(5.00);
        $this->phpexcel->getActiveSheet()->getColumnDimension('B')->setWidth(25.00);
        $this->phpexcel->getActiveSheet()->getColumnDimension('C')->setWidth(40.00);
        $this->phpexcel->getActiveSheet()->getColumnDimension('D')->setWidth(15.00);
        $this->phpexcel->getActiveSheet()->getColumnDimension('E')->setWidth(20.00);
        $this->phpexcel->getActiveSheet()->getColumnDimension('F')->setWidth(20.00);
        //set data
        $row = 5;
        $no = 1;
        foreach ($rs_id as $val) {
            $this->phpexcel->setActiveSheetIndex(0)->setCellValue('A' . $row, $no++);
            $this->phpexcel->setActiveSheetIndex(0)->setCellValue('B' . $row, $val['barang_noinventaris']);
            $this->phpexcel->setActiveSheetIndex(0)->setCellValue('C' . $row, $val['barang_nama']);
            $this->phpexcel->setActiveSheetIndex(0)->setCellValue('D' . $row, $val['ruang_name']);
            $this->phpexcel->setActiveSheetIndex(0)->setCellValue('E' . $row, $val['barang_harga_beli'])->getStyle("E" . $row)->getNumberFormat()->setFormatCode('#,##0');
            $this->phpexcel->setActiveSheetIndex(0)->setCellValue('F' . $row, $val['barang_nilai_buku'])->getStyle("F" . $row)->getNumberFormat()->setFormatCode('#,##0');
            //style
            $this->phpexcel->setActiveSheetIndex(0)->getStyle('A' . $row)->applyFromArray($styleCenterAlignment);
            $this->phpexcel->setActiveSheetIndex(0)->getStyle('E' . $row . ':F' . $row)->applyFromArray($styleRightAlignment);
            $this->phpexcel->setActiveSheetIndex(0)->getStyle('B' . $row . ':C' . $row)->applyFromArray($styleLeftAlignment);
            $this->phpexcel->setActiveSheetIndex(0)->getStyle('A' . $row . ':F' . $row)->applyFromArray($styleText);
            $this->phpexcel->setActiveSheetIndex(0)->getStyle('A' . $row . ':F' . $row)->applyFromArray($styleBorder);
            $row++;
        }
        
        // setting
        $this->phpexcel->getActiveSheet()->getPageSetup()->setOrientation(PHPExcel_Worksheet_PageSetup::ORIENTATION_LANDSCAPE);
        $this->phpexcel->getActiveSheet()->getPageSetup()->setPaperSize(PHPExcel_Worksheet_PageSetup::PAPERSIZE_A4);
        $name = 'Nilai Aset '.$rs_detail['kategoribarang_name'];
        $this->phpexcel->getActiveSheet()->setTitle($name);
        $this->phpexcel->setActiveSheetIndex(0);
        //--
        $file_name = 'NILAI_ASET_'.strtoupper(str_replace(" ", "_", $rs_detail['kategoribarang_name'])).'_'.date("d").'_'.date("m").'_'.date("Y");
        //--
//            header('Content-Type: application/vnd.ms-excel');
        header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
        header('Content-Disposition: attachment;filename=' . $file_name . '.xlsx');
        header('Cache-Control: max-age=0');
        // output
        $obj_writer = PHPExcel_IOFactory::createWriter($this->phpexcel, 'Excel2007');
        $obj_writer->save('php://output');
    }
    

    public function barang_per_lokasi() {
        $this->_set_page_rule("R");

        // set template content
        $this->smarty->assign("template_content", "laporan/barang/barang_per_lokasi.html");

        //data 
        $result = $this->m_lokasi->get_lokasi_barang();
        $this->smarty->assign("rs_id", $result);


        // notification
        $this->tnotification->display_notification();
        $this->tnotification->display_last_field();
        // output
        parent::display();
    }

     public function barang_per_lokasi_detail($id) {
        $this->_set_page_rule("R");

        // set template content
        $this->smarty->assign("template_content", "laporan/barang/barang_per_lokasi_detail.html");

        //data 
        $search = $this->tsession->userdata('search_lokasi');
        $search['kategoribarang_cd'] = empty($search['kategoribarang_cd']) ? "" : $search['kategoribarang_cd'];
        $search['jenisbarang_cd'] = empty($search['jenisbarang_cd']) ? "" : $search['jenisbarang_cd'];
        $search['merk_cd'] = empty($search['merk_cd']) ? "" : $search['merk_cd'];
        $search['ruang_cd'] = empty($search['ruang_cd']) ? "" : $search['ruang_cd'];
        //$tgl = $tahun . "-" . $bulan;
        $params = array("%".$id."%", "%".$search['kategoribarang_cd']."%", "%".$search['jenisbarang_cd']."%", 
                    "%".$search['merk_cd']."%", "%".$search['ruang_cd']."%");
        
        //sent to display
        $this->smarty->assign("search", $search);
        
        /* start of pagination --------------------- */
        // pagination
        $config['base_url'] = site_url("laporan/barang/barang_per_lokasi_detail/".$id);
        $config['total_rows'] = $this->m_barang->count_detail_barang_by_lokasi_cd($params);
        $config['uri_segment'] = 5;
        $config['per_page'] = 20;
        $this->pagination->initialize($config);
        $pagination['data'] = $this->pagination->create_links();
        // pagination attribute
        $start = $this->uri->segment(5, 0) + 1;
        $end = $this->uri->segment(5, 0) + $config['per_page'];
        $end = (($end > $config['total_rows']) ? $config['total_rows'] : $end);
        $pagination['start'] = ($config['total_rows'] == 0) ? 0 : $start;
        $pagination['end'] = $end;
        $pagination['total'] = $config['total_rows'];
        // pagination assign value
        $this->smarty->assign("pagination", $pagination);
        $this->smarty->assign("no", $start);
        /* end of pagination ---------------------- */

        // get data
        $params = array("%". $id ."%", "%".$search['kategoribarang_cd']."%", "%".$search['jenisbarang_cd']."%", "%".$search['merk_cd']."%", "%".$search['ruang_cd']."%", ($start - 1), $config['per_page']);
        $result = $this->m_barang->get_detail_barang_by_lokasi_cd($params);
        $this->smarty->assign("rs_id", $result);
        
        //set data search
        $this->smarty->assign("rs_kategori", $this->m_kategoribarang->get_all_kategoribarang());
        $this->smarty->assign("rs_merk", $this->m_merk->get_all_merk());
        $this->smarty->assign("rs_ruang", $this->m_ruang->get_all_ruang());
        $this->smarty->assign("rs_jenis", $this->m_jenisbarang->get_all_jenisbarang());

        //detail url
        $rs_detail = $this->m_lokasi->get_lokasi_by_id($id);
        $this->smarty->assign("rs_detail", $rs_detail);
                
        // notification
        $this->tnotification->display_notification();
        $this->tnotification->display_last_field();
        // output
        parent::display();
    }
    
    // proses pencarian
    public function proses_search() {
        // set page rules
        $this->_set_page_rule("R");
        // data
        if ($this->input->post('save') == "Reset") {
            $this->tsession->unset_userdata('search_lokasi');
        } else {
            $params = array(
                "kategoribarang_cd" => $this->input->post("kategoribarang_cd"),
                "jenisbarang_cd" => $this->input->post("jenisbarang_cd"),
                "merk_cd" => $this->input->post("merk_cd"),
                "ruang_cd" => $this->input->post("ruang_cd")
            );
            $this->tsession->set_userdata("search_lokasi", $params);
        }
        //echo $this->input->post("jenisbarang_cd");
        // redirect
        redirect("laporan/barang/barang_per_lokasi_detail/" . $this->input->post("lokasi_cd"));
    }
    
    public function lokasi_excel($id){
        // set page rules
        $this->_set_page_rule("R");
        // style title header
        $styleTitleHeader = array(
            'font' => array(
                'name' => 'Times New Roman',
                'bold' => 'bold',
                'size' => 11
            ),
            'alignment' => array(
                'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER
            )
        );

        // style text
        $styleText = array(
            'font' => array(
                'name' => 'Times New Roman',
                'size' => 10
            )
        );
        

        // style desc title column
        $styleDescColumn = array(
            'font' => array(
                'name' => 'Times New Roman',
                'bold' => true,
                'size' => 10
            ),
            'alignment' => array(
                'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
                'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER
            ),
            'borders' => array(
                'allborders' => array(
                    'style' => PHPExcel_Style_Border::BORDER_THIN,
                    'color' => array('argb' => '000000'),
                ),
            )
        );

        // style content
        $styleContent = array(
            'font' => array(
                'name' => 'Times New Roman',
                'bold' => false,
                'size' => 10
            ),
            'borders' => array(
                'allborders' => array(
                    'style' => PHPExcel_Style_Border::BORDER_THIN,
                    'color' => array('argb' => '000000'),
                )
            )
        );

        // style border
        $styleBorder = array(
            'borders' => array(
                'allborders' => array(
                    'style' => PHPExcel_Style_Border::BORDER_THIN,
                    'color' => array('argb' => '000000'),
                )
            )
        );


        // style right alignment
        $styleRightAlignment = array(
            'alignment' => array(
                'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_RIGHT,
                'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER
            ),
        );

        // style left alignment
        $styleLeftAlignment = array(
            'alignment' => array(
                'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_LEFT,
                'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER
            ),
        );
        

        // style justify alignment
        $styleJustifyAlignment = array(
            'alignment' => array(
                'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_JUSTIFY,
                'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER
            ),
        );

        // style center alignment
        $styleCenterAlignment = array(
            'alignment' => array(
                'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
                'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER
            ),
        );


        // data
        $search = $this->tsession->userdata('search_lokasi');
        $search['kategoribarang_cd'] = empty($search['kategoribarang_cd']) ? "" : $search['kategoribarang_cd'];
        $search['jenisbarang_cd'] = empty($search['jenisbarang_cd']) ? "" : $search['jenisbarang_cd'];
        $search['merk_cd'] = empty($search['merk_cd']) ? "" : $search['merk_cd'];
        $search['ruang_cd'] = empty($search['ruang_cd']) ? "" : $search['ruang_cd'];
        //$tgl = $tahun . "-" . $bulan;
        $params = array("%".$id."%", "%".$search['kategoribarang_cd']."%", "%".$search['jenisbarang_cd']."%","%".$search['merk_cd']."%", "%".$search['ruang_cd']."%");
        $rs_id = $this->m_barang->get_all_barang_by_lokasi_cd($params);
        $rs_detail = $this->m_lokasi->get_lokasi_by_id($id);
        
        $this->phpexcel->setActiveSheetIndex(0)->getStyle('A')->getAlignment()->setWrapText(true);
        $this->phpexcel->setActiveSheetIndex(0)->getStyle('B')->getAlignment()->setWrapText(true);
        $this->phpexcel->setActiveSheetIndex(0)->getStyle('C')->getAlignment()->setWrapText(true);
        $this->phpexcel->setActiveSheetIndex(0)->getStyle('D')->getAlignment()->setWrapText(true);
        $this->phpexcel->setActiveSheetIndex(0)->getStyle('E')->getAlignment()->setWrapText(true);
        $this->phpexcel->setActiveSheetIndex(0)->getStyle('F')->getAlignment()->setWrapText(true);
        $this->phpexcel->setActiveSheetIndex(0)->getStyle('G')->getAlignment()->setWrapText(true);
        $this->phpexcel->setActiveSheetIndex(0)->getStyle('H')->getAlignment()->setWrapText(true);
        $this->phpexcel->setActiveSheetIndex(0)
                ->setCellValue('A1', "STMIK AMIKOM YOGYAKARTA")
                ->setCellValue('A2', "LAPORAN DATA BARANG")
                ->setCellValue('A3', "LOKASI : ".strtoupper($rs_detail['lokasi_name']))
                ->setCellValue('A5', 'No')
                ->setCellValue('B5', 'No Inventaris')
                ->setCellValue('C5', 'Nama Barang')
                ->setCellValue('D5', 'Kategori')
                ->setCellValue('E5', 'Jenis')
                ->setCellValue('F5', 'Merk')
                ->setCellValue('G5', 'Lokasi')
                ->setCellValue('H5', 'Ruang');
        $this->phpexcel->getActiveSheet()
                ->mergeCells('A1:H1')
                ->mergeCells('A2:H2')
                ->mergeCells('A3:H3')
                ->mergeCells('A4:H4');
        $this->phpexcel->setActiveSheetIndex(0)->getStyle('A1:H4')->applyFromArray($styleTitleHeader);
        $this->phpexcel->setActiveSheetIndex(0)->getStyle('A5:H5')->applyFromArray($styleDescColumn);
        // set width
        $this->phpexcel->getActiveSheet()->getColumnDimension('A')->setWidth(5.00);
        $this->phpexcel->getActiveSheet()->getColumnDimension('B')->setWidth(20.00);
        $this->phpexcel->getActiveSheet()->getColumnDimension('C')->setWidth(30.00);
        $this->phpexcel->getActiveSheet()->getColumnDimension('D')->setWidth(15.00);
        $this->phpexcel->getActiveSheet()->getColumnDimension('E')->setWidth(15.00);
        $this->phpexcel->getActiveSheet()->getColumnDimension('F')->setWidth(15.00);
        $this->phpexcel->getActiveSheet()->getColumnDimension('G')->setWidth(15.00);
        $this->phpexcel->getActiveSheet()->getColumnDimension('H')->setWidth(15.00);
        //set data
        $row = 6;
        $no = 1;
        foreach ($rs_id as $val) {
            $this->phpexcel->setActiveSheetIndex(0)->setCellValue('A' . $row, $no++);
            $this->phpexcel->setActiveSheetIndex(0)->setCellValue('B' . $row, $val['barang_noinventaris']);
            $this->phpexcel->setActiveSheetIndex(0)->setCellValue('C' . $row, $val['barang_nama']);
            $this->phpexcel->setActiveSheetIndex(0)->setCellValue('D' . $row, $val['kategoribarang_name']);
            $this->phpexcel->setActiveSheetIndex(0)->setCellValue('E' . $row, $val['jenisbarang_name']);
            $this->phpexcel->setActiveSheetIndex(0)->setCellValue('F' . $row, $val['merk_name']);
            $this->phpexcel->setActiveSheetIndex(0)->setCellValue('G' . $row, $val['ruang_name']);
            $this->phpexcel->setActiveSheetIndex(0)->setCellValue('H' . $row, $val['barang_kondisi']);
            //style
            $this->phpexcel->setActiveSheetIndex(0)->getStyle('A' . $row)->applyFromArray($styleCenterAlignment);
            $this->phpexcel->setActiveSheetIndex(0)->getStyle('B' . $row . ':H' . $row)->applyFromArray($styleLeftAlignment);
            $this->phpexcel->setActiveSheetIndex(0)->getStyle('A' . $row . ':H' . $row)->applyFromArray($styleText);
            $this->phpexcel->setActiveSheetIndex(0)->getStyle('A' . $row . ':H' . $row)->applyFromArray($styleBorder);
            $row++;
        }
        
        // setting
        $this->phpexcel->getActiveSheet()->getPageSetup()->setOrientation(PHPExcel_Worksheet_PageSetup::ORIENTATION_LANDSCAPE);
        $this->phpexcel->getActiveSheet()->getPageSetup()->setPaperSize(PHPExcel_Worksheet_PageSetup::PAPERSIZE_A4);
        $name = 'Data Barang '.$rs_detail['lokasi_name'];
        $this->phpexcel->getActiveSheet()->setTitle($name);
        $this->phpexcel->setActiveSheetIndex(0);
        //--
        $file_name = 'DATA_BARANG_LOKASI_'.strtoupper(str_replace(" ", "_", $rs_detail['lokasi_name'])).'_'.date("d").'_'.date("m").'_'.date("Y");
        //--
//            header('Content-Type: application/vnd.ms-excel');
        header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
        header('Content-Disposition: attachment;filename=' . $file_name . '.xlsx');
        header('Cache-Control: max-age=0');
        // output
        $obj_writer = PHPExcel_IOFactory::createWriter($this->phpexcel, 'Excel2007');
        $obj_writer->save('php://output');
    }
    
    /*
     * CHART
     */

    public function data_xml_barang_per_kategori() {
        $rs_aircraft = $this->m_kategoribarang->get_all_kategori_graph();

        $max = 10;
        $data = '';
        $no = 0;
        $color = array("AFD8F8", "F6BD0F", "8BBA00", "FF8E46", "008E8E", "D64646", "8E468E", "588526", "B3AA00", "008ED6", "9D080D", "A186BE");
        header("Content-type: text/xml");
        foreach ($rs_aircraft as $key => $value) {
            if ($max < $value['total_barang']) {
                $max = $value['total_barang'];
            }
            $data .= "<set name='" . $value['kategoribarang_name'] . "' value='" . $value['total_barang'] . "' color='" . $color[$no++] . "'/>";
            //echo "dasdsa".$color[$no++];
        }
        $data_xml = '<chart showNames="1" decimalPrecision="0" bgColor="FFFFFF" xAxisName="Month" yAxisName="Units" formatNumberScale="0">';
        $data_xml .= $data;
        $data_xml .= "</chart>";

        $data_xml = '<graph caption="" xAxisName="Total" yAxisName="Kategori" decimalPrecision="0" formatNumberScale="0">';
        $data_xml .= $data;
        $data_xml .= "</graph>";

        echo $data_xml;
    }

}
